package com.miracle.设计模式.结构型模式.代理模式.CGLIB动态代理;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

class HttpInvokeProxy3 implements MethodInterceptor {

    private Object object;


    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object obj = methodProxy.invoke(object, objects);
        after();
        return obj;
    }

    public Object getProxy(Object object) {
        this.object = object;
        //1.工具类
        Enhancer enhancer = new Enhancer();
        //2.设置父类
        enhancer.setSuperclass(this.object.getClass());
        //3.设置回调函数
        enhancer.setCallback(this);
        //4.设置子类(代理对象)
        Object o = enhancer.create();
        return o;
    }

    /**
     * 前置处理
     */
    private void before() {
        System.out.println("前置加工");
    }

    /**
     * 后置处理
     */
    private void after() {
        System.out.println("后置加工");
    }

}
